<template>
  <!-- 区域通行记录 -->
  <div class="swipe_details">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="区域:">
        <el-select
          v-model="defaultArea"
          placeholder="请选择"
          size="mini"
          clearable
          @change="selectArea"
        >
          <el-option
            v-for="item in areaInfoList"
            :key="item.areaGuid"
            :label="item.areaName"
            :value="item.areaGuid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋:">
        <el-select
          v-model="defaultBuilding"
          placeholder="请选择"
          size="mini"
          clearable
          @change="selectBuilding"
        >
          <el-option label="全部类型" value></el-option>
          <el-option
            v-for="item in buildingInfoList"
            :key="item.buildingGuid"
            :label="item.buildingName"
            :value="item.buildingGuid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间:">
        <el-input v-model="roomName" placeholder="房间" size="mini" clearable style="width: 150px"></el-input>
      </el-form-item>
      <el-form-item label="姓名/编号/卡号:">
        <el-input
          v-model="userName"
          placeholder="姓名/编号/卡号"
          size="mini"
          clearable
          style="width: 150px"
        ></el-input>
      </el-form-item>
      <el-form-item label="类型:">
        <el-select
          v-model="cardtypes"
          placeholder="请选择"
          size="mini"
          clearable
          @change="selectdepartment"
        >
          <el-option label="全部类型" value></el-option>
          <el-option
            v-for="item in cardtypesoption"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
        ></el-time-picker>
      </el-form-item>
      <el-form-item class="searchmarigin">
        <el-button type="primary" @click="onSubmit" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onExport" size="mini" icon="el-icon-document-copy">导出</el-button>
      </el-form-item>
    </el-form>
    <div class="tablemain">
      <el-table
        v-loading="loading"
        :data="detailslist"
        ref="table"
        border
        highlight-current-row
        style="width: 100%"
        :header-cell-style="headFirst"
        :cell-style="(res) => headFirst_col(res, [])"
      >
        <el-table-column
          prop="cardNumber"
          label="卡号"
          v-if="formThead.cardNumber.checked"
          min-width="120px"
        ></el-table-column>
        <el-table-column
          prop="userNumber"
          label="编号"
          v-if="formThead.userNumber.checked"
          min-width="110px"
        ></el-table-column>
        <el-table-column prop="userName" label="姓名" v-if="formThead.userName.checked"></el-table-column>
        <el-table-column prop="dataMakeTime" label="通行时间" min-width="160px"></el-table-column>
        <el-table-column prop="place" label="所在位置"></el-table-column>
        <el-table-column prop="roomName" label="房间"></el-table-column>
        <el-table-column prop="dataType" label="开门类型">
          <template slot-scope="scope">
            {{
            scope.row.dataType | cardtypesoptionType
            }}
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChangepage"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="recordCount"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
export default {
  data() {
    return {
      // 用户id
      userGuid: '',
      filename: '门锁区域通行记录',
      // 区域信息
      defaultArea: '',
      areaInfoList: [],
      // 楼栋信息
      defaultBuilding: '',
      buildingInfoList: [],
      // 自己输入的房间号
      roomName: '',
      // 部门id
      departmentGuid: '',
      // 姓名/编号/卡号
      userName: '',
      // 日期数组
      datetime: [],
      time: [],
      // 类型
      cardtypes: '',
      // 类型列表
      cardtypesoption: [
        {
          label: '正常刷卡',
          value: 1
        },
        {
          label: '非法卡',
          value: 2
        },
        {
          label: '钥匙开门',
          value: 3
        },
        {
          label: '远程开门',
          value: 4
        },
        {
          label: '远程常开',
          value: 5
        },
        {
          label: '远程闭门',
          value: 6
        },
        {
          label: '反锁',
          value: 7
        },
        {
          label: '解除反锁',
          value: 8
        },
        // {
        //   label: "闭门",
        //   value: 9
        // },
        {
          label: '假锁',
          value: 10
        },
        {
          label: '卡格式错误',
          value: 11
        },
        {
          label: '脱机授权记录',
          value: 12
        },
        {
          label: '名单卡记录',
          value: 13
        },
        {
          label: '时效密码开门',
          value: 14
        },
        {
          label: '临时密码开门',
          value: 15
        },
        {
          label: '密码开门',
          value: 21
        },
        {
          label: '超级密码开门',
          value: 22
        },
        {
          label: '胁迫密码开门',
          value: 23
        },
        {
          label: '电量数据',
          value: 25
        },
        {
          label: '远程关门',
          value: 34
        },
        {
          label: '远程取消常开',
          value: 35
        },
        {
          label: '远程取消封门',
          value: 36
        },
        {
          label: '用户指纹认证开门',
          value: 41
        },
        {
          label: '管理员指纹认证开门',
          value: 42
        },
        {
          label: '增加指纹',
          value: 45
        },
        {
          label: '配置卡',
          value: 252
        },
        {
          label: '授权卡',
          value: 253
        }
      ],
      elform: {},
      // 区域数据
      detailslist: [],
      // 表格加载
      loading: false,
      pageNum: 1,
      pageSize: 20,
      currentPage: 1,
      recordCount: 0,
      // 查询条件
      searchInfo: {},
      // 头部
      formThead: {
        cardNumber: { label: '卡号', prop: 'cardNumber', checked: true },
        userNumber: { label: '编号', prop: 'userNumber', checked: true },
        userName: { label: '姓名', prop: 'userName', checked: true },
        dataMakeTime: {
          label: '通行时间',
          prop: 'dataMakeTime',
          checked: true
        },
        place: { label: '所在位置', prop: 'place', checked: true },
        roomName: { label: '房间', prop: 'roomName', checked: true },
        dataType: { label: '开门类型', prop: 'dataType', checked: true },
        departmentName: {
          label: '部门',
          prop: 'departmentName',
          checked: true
        },
        chargingType: {
          label: '交易类型',
          prop: 'chargingType',
          checked: true
        },
        expenditure: { label: '操作金额', prop: 'expenditure', checked: true },
        originBalance: {
          label: '卡内余额',
          prop: 'originBalance',
          checked: true
        }
      },
      //导出数据
      detailslistExportlist: [],
      //汇总合计
      totalto: false
    }
  },
  filters: {},
  props: ['roomGuid'],
  created() {
    this.userGuid = sessionStorage.getItem('administrator')
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  mounted() {
    this.getAreaInfo()
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  methods: {
    selectdepartment() {
      console.log(this.cardtypes)
    },
    //合计查询
    searchtotal() {},
    // 区域查询
    getAreaInfo() {
      this.$http({
        url: this.$http.adornUrl('/area/room/area-info-search'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            unitGuid: this.changeschoolid,
            userGuid: this.userGuid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.areaInfoList = data.messageContent
          if (this.areaInfoList.length != 0) {
            this.defaultArea = this.areaInfoList[0].areaGuid
          }

          console.log(data.messageContent)
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 选择区域后触发
    selectArea() {
      console.log(this.defaultArea)
      if (this.defaultArea == '' || this.defaultArea == null) {
        console.log(1)
        return
      }
      this.defaultBuilding = ''
      // 获取对应区域内的楼栋信息
      this.$http({
        url: this.$http.adornUrl('/area/room/get-area-building'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            areaGuid: this.defaultArea
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.buildingInfoList = data.messageContent
          console.log(data.messageContent)
        } else {
          this.$message.error(data.errorMessage)
        }
      })
      // this.getAreaInfo();
    },
    // 选择楼栋后触发
    selectBuilding() {},
    // 选择时间后触发
    selecttime() {
      console.log(this.defaultArea)
    },
    // 点击查询后触发
    onSubmit() {
      let searchInfo = {}
      console.log(this.datetime)
      if (this.defaultArea == '') {
        this.$message({
          message: '区域必须选择',
          type: 'warning'
        })
        return false
      } else {
        searchInfo.areaGuid = this.defaultArea
        searchInfo.unitGuid = this.changeschoolid
        console.log(this.changeschoolid)
      }
      if (this.defaultBuilding != '') {
        searchInfo.buildingGuid = this.defaultBuilding
      }
      // 姓名/编号/卡号  规则
      if (this.userName != '') {
        searchInfo.userData = this.userName
      }
      // 日期限定
      if (this.datetime != null) {
        if (this.datetime.length != 0) {
          searchInfo.startTime = this.datetime[0] + ' ' + this.time[0]
          searchInfo.endTime = this.datetime[1] + ' ' + this.time[1]
        }
      }
      console.log(this.searchInfo)
      // 类型限定
      if (this.cardtypes != '') {
        searchInfo.dataType = this.cardtypes
      }
      // 房间限定
      if (this.roomName != '') {
        searchInfo.roomName = this.roomName
      }
      this.searchInfo = searchInfo
      // 区域限定
      console.log(searchInfo)
      this.pageNum = 1
      this.getDoorList()
    },
    getDoorList() {
      this.$http({
        url: this.$http.adornUrl(
          '/terminal/doorLock/lock-date-area-search-page'
        ),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            asc: true,
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.recordCount = data.messageContent.recordCount
          this.detailslist = data.messageContent.data
          this.currentPage = data.messageContent.pageNum
          if (data.messageContent.data.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 导出数据
    onExport() {
      if (this.recordCount == 0) {
        this.$message({
          message: '无数据可导出',
          type: 'warning'
        })
        return false
      }
      this.$http({
        url: this.$http.adornUrl(
          '/terminal/doorLock/lock-date-area-search-page'
        ),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            asc: true,
            pageNum: 1,
            pageSize: this.recordCount,
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.outexecelinit(data.messageContent.data)
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    // 通行记录导出方法
    outexecelinit(detailslist) {
      console.log('导出')
      const nowtime = this.getimeto()
      detailslist.map(item => {
        this.cardtypesoption.map(itemOption => {
          if (item.dataType == itemOption.value) {
            item.dataType = itemOption.label
            return
          }
        })
      })
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../excel/Export2Excel')
        let tHeader = [
          '卡号',
          '编号',
          '姓名',
          '通行时间',
          '所在位置',
          '房间',
          '开门类型'
        ]

        const filterVal = Object.keys(this.detailslist[0])
        const list = detailslist
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDoorList()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.getDoorList()
    }
  }
}
</script>

<style lang="scss" scoped>
.swipe_details {
  padding: 10px;
  //滚动条的宽度
  ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }

  //滚动条的滑块
  ::-webkit-scrollbar-thumb {
    background-color: #a1a3a9;
    border-radius: 3px;
  }

  .el-table__row .el-table__row--level-1 {
    background-color: #e5e9f2;
  }
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 340px;
    }
  }
  .demo-form-inline1 {
    border-bottom: 1px solid #e5e9f2;
    .searchmarigin {
      margin-left: 20px;
    }
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>